package com.example.an.service_exm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static MainActivity appRef = null;
    private Button btn1,btn2;
    boolean k=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appRef=this;
        setContentView(R.layout.activity_main);
        btn1= (Button) findViewById(R.id.click);
        btn2= (Button) findViewById(R.id.exit);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);


    }
    public static MainActivity getAppRef(){
        return appRef;
    }
    public void btEvent(String data){
        setTitle(data);
    }

    @Override
    public void onClick(View view) {
        if (view==btn1){
            setTitle("Waiting…………Alarm=5");
            Intent intent=new Intent(MainActivity.this,Recevier.class);
            PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
            Calendar calendar= Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 3);
            AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
        }
        if (view==btn2){
            Intent intent=new Intent(MainActivity.this,Recevier.class);
            PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
            AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
            am.cancel(pendingIntent);
            finish();
        }
    }
}
